import { strategyArmory } from "@/apis";

// 提取常量
const ALERT_MESSAGE = "请在请求地址中，配置 strategyId 值，如：http://localhost:3000/?strategyId=100006";
const ERROR_MESSAGE = "抽奖策略装配失败 code: {code} info: {info}";

// 提取样式
const buttonStyles = {
    className: "px-6 py-2 mb-8 text-white bg-blue-500 rounded-full shadow-lg hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-300",
    style: { cursor: "pointer" }
};

export function StrategyArmory() {
    const strategyArmoryHandle = async () => {
        const queryParams = new URLSearchParams(window.location.search);
        const strategyId = Number(queryParams.get('strategyId'));

        if (!strategyId) {
            window.alert(ALERT_MESSAGE);
            return;
        }

        try {
            const res = await strategyArmory(strategyId);
            const { code, info } = await res.json();

            if (code !== "0000") {
                window.alert(ERROR_MESSAGE.replace("{code}", code).replace("{info}", info));
                return;
            }
        } catch (error) {
            window.alert("请求失败，请检查网络连接或稍后再试");
            console.error(error);
        }
    };

    return (
        <div
            {...buttonStyles}
            onClick={strategyArmoryHandle}
        >
            装配抽奖
        </div>
    );
}
